package com.infoland.afanmall.model.story.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;


import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.view.video.MyJiaoZiVideo;
import com.infoland.afanmall.model.story.entity.StoryEntity;
import com.infoland.afanmall.model.story.fragment.StoryFragment;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;

import java.util.List;

import cn.jzvd.JZVideoPlayerStandard;


public class StoryAdapter extends BaseMultiItemQuickAdapter<StoryEntity.ObjectBean.ListBean, BaseViewHolder> implements
        BaseQuickAdapter.SpanSizeLookup {
    private int layoutPosition = 0;
    private Context context;
    private RecyclerView recyclerView;
    private int statusBarHeight;

    public StoryAdapter(List<StoryEntity.ObjectBean.ListBean> data, Context context, RecyclerView recyclerView, int statusBarHeight) {
        super(data);
        this.context = context;
        this.recyclerView = recyclerView;
        this.statusBarHeight = statusBarHeight;
        addItemType(0, R.layout.story_item_video);//视频 0
        addItemType(1, R.layout.story_item_img_text);//图片 1
    }

    @Override
    protected void convert(BaseViewHolder holder, StoryEntity.ObjectBean.ListBean item) {
        //判断当前故事item是哪个布局，0视频，1图片
        switch (holder.getItemViewType()) {
            case 0:
                initStoryVideo(holder, item);
                LogKit.E("  initStoryVideo---> ", "  " + item.toString());
                break;
            case 1:
                initStoryImage(holder, item);
                break;
        }
    }

    private void initStoryImage(BaseViewHolder holder, StoryEntity.ObjectBean.ListBean item) {
//        第一行加边距
        if (holder.getAdapterPosition() == 0) {
            ToolsKit.addMargin(holder.getView(R.id.story_image), 0,
                    statusBarHeight + SizeUtils.dp2px(9), 0, 0);
        }
        ToolsKit.setText(holder.getView(R.id.story_test_big), item.getS_title());
        ToolsKit.setTextColor(holder.getView(R.id.story_test_big), item.getS_title_color());
        ToolsKit.setText(holder.getView(R.id.story_test_small), item.getS_description());
        holder.getView(R.id.story_image).setOnClickListener(v -> {
            callBack.onClick(v, "img", 0, holder.getAdapterPosition(), item);
        });
        ToolsKit.setImgOverride(holder.getView(R.id.story_img), item.getS_background_image(), holder.getView(R.id.story_img));
//        ToolsKit.setImg(item.getS_background_image(), holder.getView(R.id.story_img));
    }

    private void initStoryVideo(BaseViewHolder holder, StoryEntity.ObjectBean.ListBean item) {
        if (holder.getAdapterPosition() == 0) {
            ToolsKit.addMargin(holder.getView(R.id.video), 0, statusBarHeight +
                    SizeUtils.dp2px(9), 0, 0);
        }
        ToolsKit.setText(holder.getView(R.id.story_test_big), item.getS_title());
        ToolsKit.setTextColor(holder.getView(R.id.story_test_big), item.getS_title_color());
        ToolsKit.setText(holder.getView(R.id.story_test_small), item.getS_description());
        View view = holder.getView(R.id.story_video_item_ll);
        view.setOnClickListener(v -> {
            callBack.onClick(v, "video", 4, holder.getAdapterPosition(), item);
        });
        if (item.getBase_browse() > 9999) {
            ToolsKit.setText(holder.getView(R.id.story_test_see), "9999+");
        } else {
            ToolsKit.setText(holder.getView(R.id.story_test_see), item.getBase_browse());
        }
        if (item.getBrowse() > 9999) {
            ToolsKit.setText(holder.getView(R.id.story_test_message), "9999+");
        } else {
            ToolsKit.setText(holder.getView(R.id.story_test_message), item.getBrowse());
        }

        //设置浏览量（小眼睛）点击事件
        holder.getView(R.id.story_see_img).setOnClickListener(v -> {
            callBack.onClick(v, "video", 0, holder.getAdapterPosition(), item);
        });
        //设置评论点击事件
        holder.getView(R.id.story_message_img).setOnClickListener(v -> {
            callBack.onClick(v, "video", 1, holder.getAdapterPosition(), item);
        });
        //设置分享点击事件
        holder.getView(R.id.story_share_img).setOnClickListener(v -> {
            callBack.onClick(v, "video", 2, holder.getAdapterPosition(), item);
        });
        MyJiaoZiVideo video = holder.getView(R.id.story_video);
        ToolsKit.setVideo(video, item.getS_background_video());
        ToolsKit.setVideoImg(video, item.getS_background_image());
        video.setVideoImgRadius(15, 15, 0, 0);
        video.autoQuitFullscreen();
//        JZVideoPlayerStandard.full
//        video.autoQuitFullscreen();
    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return 0;
    }

    private StoryCallBack callBack;

    public interface StoryCallBack {
        void onClick(View v, String img_video, int type, int position, StoryEntity.ObjectBean.ListBean item);
    }

    public void setCallBack(StoryCallBack callBack) {
        this.callBack = callBack;
    }
}
